package com.example.administrator.androidui5;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by Administrator on 2018/6/12.
 */

public class MyDialog extends Dialog {
    Context  context;//当前对话框所在的上下文环境
    EditText mEtyHeight;//输入身高的
    EditText mEtyWeight;
    Button mBtnCompute;
    Button mBtnCancel;

    MainActivity.OnComputed onComputed;
    public MyDialog(@NonNull Context context,MainActivity.OnComputed onComputed){
        super(context);
        this.context = context;//保存上下文到context
        this.onComputed = onComputed;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_dialog);
        initView();
        iniEvent();
    }

    private void iniEvent() {


        mBtnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mBtnCompute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              String heights=mEtyHeight.getText().toString();
              int height = Integer.parseInt(heights);

                String weights=mEtyWeight.getText().toString();
                int weight = Integer.parseInt(weights);

                int standardWeight = (int)((height - 80)*0.7);

                String message="";
                if(standardWeight>weight){
                    message="瘦";
                }else if(standardWeight==weight){
                    message="标准";
                }else{
                    message="偏胖";
                }
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                onComputed.showMessage(message);
            }
        });
    }

    private void initView() {
        mEtyHeight = findViewById(R.id.et_height);
        mEtyWeight = findViewById(R.id.et_weight);
        mBtnCompute = findViewById(R.id.btn_compute);
        mBtnCancel = findViewById(R.id.btncancel);
    }
}
